package com.fr.report.parameter;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.Style;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.ParameterReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.cellElement.Formula;
import com.fr.report.web.ui.FieldEditor;
import com.fr.report.web.ui.TextEditor;
import com.fr.report.web.ui.Widget;
import com.fr.report.web.ui.impl.form.FormSubmitButton;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CustomParameterUI extends AbstractParameterUI
{
  private static final int textWidth = 80;
  private static final int editorWidth = 140;
  private static final int pCount = 3;

  public CustomParameterUI()
  {
    this(null);
  }

  public CustomParameterUI(ParameterReport paramParameterReport)
  {
    setParameterForm(paramParameterReport);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    this.parameterForm = new ParameterReport();
    paramXMLableReader.readXMLObject(this.parameterForm);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (this.parameterForm != null)
      this.parameterForm.writeXML(paramXMLPrintWriter);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  public static ParameterReport createEmptyParameterForm(TemplateWorkBook paramTemplateWorkBook)
  {
    ParameterReport localParameterReport = new ParameterReport();
    DefaultCellElement localDefaultCellElement = new DefaultCellElement(0, 0, Inter.getLocText("Query"));
    localDefaultCellElement.setWidget(new FormSubmitButton());
    localParameterReport.addCellElement(localDefaultCellElement);
    return localParameterReport;
  }

  public static ParameterUI createDefaultParameterUI()
  {
    CustomParameterUI localCustomParameterUI = new CustomParameterUI();
    localCustomParameterUI.setParameterForm(createEmptyParameterForm());
    return localCustomParameterUI;
  }

  public static ParameterUI createDefaultParameterUI(TemplateWorkBook paramTemplateWorkBook, Map paramMap1, Map paramMap2)
  {
    CustomParameterUI localCustomParameterUI = new CustomParameterUI();
    localCustomParameterUI.setParameterForm(createDefaultParameterForm(paramTemplateWorkBook, paramMap1, paramMap2));
    return localCustomParameterUI;
  }

  public static ParameterReport createDefaultParameterForm(TemplateWorkBook paramTemplateWorkBook, Map paramMap1, Map paramMap2)
  {
    if (paramTemplateWorkBook == null)
      return null;
    Parameter[] arrayOfParameter = new Parameter[0];
    try
    {
      arrayOfParameter = paramTemplateWorkBook.getParameters();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    if (ArrayUtils.isEmpty(arrayOfParameter))
      return createEmptyParameterForm();
    ParameterReport localParameterReport = createDefaultParameterForm(arrayOfParameter, paramMap1, paramMap2);
    return localParameterReport;
  }

  private static ParameterReport createEmptyParameterForm()
  {
    ParameterReport localParameterReport = new ParameterReport();
    DefaultCellElement localDefaultCellElement = new DefaultCellElement(0, 0, Inter.getLocText("Query"));
    localDefaultCellElement.setWidget(new FormSubmitButton());
    localParameterReport.addCellElement(localDefaultCellElement);
    return localParameterReport;
  }

  public static ParameterReport createDefaultParameterForm(Parameter[] paramArrayOfParameter, Map paramMap1, Map paramMap2)
  {
    ParameterReport localParameterReport = new ParameterReport();
    HashSet localHashSet = new HashSet();
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < paramArrayOfParameter.length; ++i)
    {
      Parameter localParameter1 = paramArrayOfParameter[i];
      if (localParameter1 != null)
      {
        String str1 = localParameter1.getName();
        if ((str1 != null) && (!(localHashSet.contains(str1.toUpperCase()))))
        {
          localArrayList.add(paramArrayOfParameter[i]);
          localHashSet.add(str1.toUpperCase());
        }
      }
    }
    localParameterReport.setHeader(0, null);
    localParameterReport.setFooter(0, null);
    Style localStyle = Style.DEFAULT_STYLE.deriveHorizontalAlignment(4);
    int j = 0;
    int k = 0;
    int l = localArrayList.size();
    for (int i1 = 0; i1 < l; ++i1)
    {
      Parameter localParameter2 = (Parameter)localArrayList.get(i1);
      j = i1 % 3 * 2;
      k = i1 / 3 * 2 + 1;
      if (i1 % l == 0)
        localParameterReport.setRowHeight(k, 23);
      localParameterReport.setRowHeight(k - 1, 10);
      String str2 = localParameter2.getName();
      if (StringUtils.isBlank(str2))
        str2 = localParameter2.getName();
      DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(j, k, str2 + ":");
      localParameterReport.setColumnWidth(j, 80);
      localDefaultCellElement2.setStyle(localStyle);
      localParameterReport.addCellElement(localDefaultCellElement2);
      DefaultCellElement localDefaultCellElement3 = new DefaultCellElement(j + 1, k, alterCellValue(localParameter2.getValue()));
      localParameterReport.setColumnWidth(j + 1, 140);
      Object localObject = null;
      if (paramMap1 != null)
        localObject = (FieldEditor)paramMap1.get(localParameter2.getName());
      if (localObject == null)
        localObject = new TextEditor();
      ((FieldEditor)localObject).setWidgetName(localParameter2.getName());
      localDefaultCellElement3.setWidget((Widget)localObject);
      if ((paramMap2 != null) && (paramMap2.containsKey(localParameter2.getName())))
        localDefaultCellElement3.setValue(paramMap2.get(localParameter2.getName()));
      localParameterReport.addCellElement(localDefaultCellElement3);
    }
    localParameterReport.setRowHeight(k + 1, 10);
    DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(1, k + 2, 6, 1, null);
    localDefaultCellElement1.setWidget(new FormSubmitButton(Inter.getLocText("Query")));
    localDefaultCellElement1.setStyle(localStyle.deriveHorizontalAlignment(4));
    localParameterReport.addCellElement(localDefaultCellElement1);
    localParameterReport.setRowHeight(k + 2, 23);
    localParameterReport.addCellElement(new DefaultCellElement(0, k + 3));
    localParameterReport.setRowHeight(k + 3, 10);
    return ((ParameterReport)localParameterReport);
  }

  private static Object alterCellValue(Object paramObject)
  {
    if ((paramObject instanceof String) && (((String)paramObject).startsWith("=")))
      paramObject = new Formula((String)paramObject);
    return paramObject;
  }
}